import QtQuick 2.0
import QtQuick.Controls 1.2
import QtQuick.Window 2.0
import SceneGraphRendering 1.0

import Utils 1.0

Rectangle {
    id: mapaRoot
    width: parent.width
    height: Screen.height*90/100;

    property string titulo: "Mapa"
    property string subTitulo: null

    property var mapaContent: null

    property string pos

    WebviewUtils {
        id: wvUtils
        onRequestReceived: {
            console.log("REQ: "+req);
            var index = req.indexOf(":");

            var key = req.substring(0,index);
            var val = req.substring(index+1);

            if(key=="newPos") {
                applicationWindow.setCurrentPos(val);
            } else if(key=="markerClicked") {
                applicationWindow.exibeFornecedores();
            }
        }
    }

    Button {
        onClicked: {
            addMarker(1, -34.397, 150.644, 'teste do pin');
        }
    }

    OffscreenWebView {
        id: webView
        width: parent.width
        height: parent.height
        property bool startPage: true

        Component.onCompleted: {
            mapaRoot.mapaContent = wvUtils.readTextFile(":///html/map.html");
            webView.loadData(mapaRoot.mapaContent);
        }

        onPageStarted: {
            if(url.indexOf("data:")==0) {
                startPage = true;
            } else {
                startPage = false;
            }
        }

        function goBack() {
            webView.loadUrl("javascript:window.history.back();");
        }
    }

    function reload() {
        webView.loadData(mapaRoot.mapaContent);
    }

    function back() {
        if(webView.startPage) return true;

        webView.goBack();
        return false;
    }

}
